package sun.duandianxiazai;

import android.content.Intent;
import android.os.Environment;
import android.util.Log;

import com.example.johnsun.mytestapp.my.utils.MyAppliacation;

import java.net.MalformedURLException;

/**
 * Created by JohnSun on 2016/3/23.
 */
public class TestDownloadTask {

    private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/xun";
    public  static void testSomething(){


        try {

            DownloadTaskListener taskListener =new DownloadTaskListener() {
                @Override
                public void updateProcess(DownloadTask task) {
                    Intent updateIntent = new Intent(
                            "com.yyxu.download.activities.DownloadListActivity");
                    updateIntent.putExtra(MyIntents.TYPE, MyIntents.Types.PROCESS);
                    updateIntent.putExtra(MyIntents.PROCESS_SPEED, task.getDownloadSpeed() + "kbps | "
                            + task.getDownloadSize() + " / " + task.getTotalSize());
                    updateIntent.putExtra(MyIntents.PROCESS_PROGRESS, task.getDownloadPercent() + "");
                    updateIntent.putExtra(MyIntents.URL, task.getUrl());
                    MyAppliacation.maPP.sendBroadcast(updateIntent);

                }

                @Override
                public void finishDownload(DownloadTask task) {

                    Log.i("下载完了  ",task.getFile().getName());

                }

                @Override
                public void preDownload(DownloadTask task) {

                }

                @Override
                public void errorDownload(DownloadTask task, Throwable error) {

                    Log.i("错误  ",task.getFile().getName());
                    Log.i("错误  ",error.toString());

                }
            };
         //   String url = URLEncoder.encode("http://101.200.177.122:8080/childProject/upload/28/1_当你高兴的时候 你会笑.png", "UTF-8");
            String url ="http://101.200.177.122:8080/childProject/upload/28/害怕教授1.mp3";
            String s=url.replaceAll(" ","%20");
            new DownloadTask(MyAppliacation.maPP,s,
                    SDCARD_ROOT,taskListener).execute();
        } catch (MalformedURLException e){
            e.printStackTrace();
        }
    }

}
